package slim3.model;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.JDOHelper;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;

import org.slim3.util.DateUtil;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@Version(strategy = VersionStrategy.VERSION_NUMBER)
public class Task implements Serializable {

    private static final long serialVersionUID = 1L;

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String key;

    @Persistent
    private Date dueDate;

    @Persistent
    private String content;

    @Persistent
    private Boolean finished;

    @Persistent
    private String categoryKey;

    // @NotPersistent
    // private Category category;

    public String getCategoryKey() {
        return categoryKey;
    }

    public void setCategoryKey(String categoryKey) {
        this.categoryKey = categoryKey;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Date getDueDate() {
        return dueDate;
    }

    public String getFormatDueDate() {
        return DateUtil.toString(dueDate, "yyyy/MM/dd");
    }

    // public void setDueDate(String date) {
    // System.out.println("きた。。");
    // this.dueDate = DateUtil.toDate(date, "yyyy/MM/dd");
    // }
    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Boolean getFinished() {
        return finished;
    }

    public void setFinished(Boolean finished) {
        this.finished = finished;
    }

    public void setCategory(Category category) {
        if (category == null)
            categoryKey = null; 
        this.categoryKey = category.getKey();
    }

    public Category getCategory() {
        if (categoryKey == null)
            return null;
        return JDOHelper.getPersistenceManager(this).getObjectById(
            Category.class,
            categoryKey);
    }
}
